package com.tianhui.consignortwofei.pattern.orderAdjust.impl;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;

import com.fgs.common.CommonModel;
import com.fgs.common.CommonResponse;
import com.fgs.common.api.CommonConfig;
import com.fgs.common.interf.InputItemType;
import com.fgs.common.progress.ExceptionHandle;
import com.fgs.common.progress.ObserverResponseListener;
import com.fgs.common.utils.StringUtil;
import com.fgs.common.utils.ToastUtil;
import com.tianhui.consignortwofei.api.OrderApiService;
import com.tianhui.consignortwofei.constant.KeyValue;
import com.tianhui.consignortwofei.constant.OrderType;
import com.tianhui.consignortwofei.manager.EventBusManage;
import com.tianhui.consignortwofei.mvp.model.enty.AdjustInfo;
import com.tianhui.consignortwofei.mvp.model.enty.OrderInfo;
import com.tianhui.consignortwofei.mvp.ui.activity.AdjustSuccessActivity;
import com.tianhui.consignortwofei.pattern.orderAdjust.IOrderAdjust;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * @author fgs
 * @date 2020/9/7
 * @description 路耗调整
 **/
public class RoadAdjust implements IOrderAdjust {
    @Override
    public void adjustOperate(Context context, AdjustInfo adjustInfo, OrderInfo orderInfo) {


        if (TextUtils.isEmpty(adjustInfo.adjustValue)) {
            ToastUtil.showShortToast("请输入路耗计量值");
            return;
        }
        Map<String, String> map = new HashMap<>();
        map.put("billno", orderInfo.billno);
        if (adjustInfo.adjustType == 0) { //定额

            String consumeWeight = new BigDecimal(adjustInfo.adjustValue).divide(new BigDecimal("1000"),2,BigDecimal.ROUND_HALF_UP).toString();

            if (StringUtil.compareTwoString(consumeWeight, orderInfo.weight) == 1) {
                ToastUtil.showShortToast("定额重量不能大于发单重量");
                return;
            }

            map.put("consumequota", adjustInfo.adjustValue);
        } else { //定率
            if (StringUtil.compareTwoString(adjustInfo.adjustValue, "1000") == 1) {
                ToastUtil.showShortToast("定率不能大于1000");
                return;
            }
            map.put("consumefixedrate", adjustInfo.adjustValue);
        }


        if (OrderType.TYPE_SOURCE_DINGJIN==orderInfo.dingJinType) {//区分有无订金
            new CommonModel().subscribe(context, CommonConfig.getInstance().create(OrderApiService.class).orderAdjustDingJin(map), new ObserverResponseListener<CommonResponse>() {
                @Override
                public void onNext(CommonResponse commonResponse) {
                    if (commonResponse.getCode() == 20000) {
                        if (context != null) {
                            ((Activity) context).finish();
                        }
                        EventBusManage.operateActivityFinish();
                        EventBusManage.refreshOrderDetail();
                        Bundle bundle = new Bundle();
                        bundle.putParcelable(KeyValue.KEY_ORDER_INFO, orderInfo);
                        bundle.putInt(KeyValue.KEY_TYPE, OrderType.TYPE_ADJUST_ROAD);
                        bundle.putString(KeyValue.KEY_VALUE, adjustInfo.adjustValue);
                        Intent intent = new Intent(context, AdjustSuccessActivity.class);
                        intent.putExtras(bundle);
                        context.startActivity(intent);
                    } else {
                        ToastUtil.showShortToast(commonResponse.getMsg());
                    }

                }

                @Override
                public void onError(ExceptionHandle.ResponseThrowable e) {
                    ToastUtil.showShortToast(e.message);
                }
            });
        }else {
            new CommonModel().subscribe(context, CommonConfig.getInstance().create(OrderApiService.class).orderAdjust(map), new ObserverResponseListener<CommonResponse>() {
                @Override
                public void onNext(CommonResponse commonResponse) {
                    if (commonResponse.getCode() == 20000) {
                        if (context != null) {
                            ((Activity) context).finish();
                        }
                        EventBusManage.operateActivityFinish();
                        EventBusManage.refreshOrderDetail();
                        Bundle bundle = new Bundle();
                        bundle.putParcelable(KeyValue.KEY_ORDER_INFO, orderInfo);
                        bundle.putInt(KeyValue.KEY_TYPE, OrderType.TYPE_ADJUST_ROAD);
                        bundle.putString(KeyValue.KEY_VALUE, adjustInfo.adjustValue);
                        Intent intent = new Intent(context, AdjustSuccessActivity.class);
                        intent.putExtras(bundle);
                        context.startActivity(intent);
                    } else {
                        ToastUtil.showShortToast(commonResponse.getMsg());
                    }

                }

                @Override
                public void onError(ExceptionHandle.ResponseThrowable e) {
                    ToastUtil.showShortToast(e.message);
                }
            });
        }
    }

    @Override
    public String getAdjustTitle() {
        return "路耗调整";
    }

    @Override
    public String getOriginalText() {
        return "路耗类型";
    }

    @Override
    public String getOriginalValue(OrderInfo orderInfo) {
        return orderInfo.consumequota;
    }

    @Override
    public String getOriginalValueInputTip() {
        return null;
    }

    @Override
    public String getAdjustText(OrderInfo orderInfo) {
        if (orderInfo != null) {
            if (!TextUtils.isEmpty(orderInfo.consumequota)) {
                return "路耗计量（定额）";
            } else if (!TextUtils.isEmpty(orderInfo.consumefixedrate)) {
                return "路耗计量（定率）";
            } else {
                return "路耗计量（定额）";
            }
        }
        return "路耗计量（定额）";
    }

    @Override
    public int getInputItemType() {
        return InputItemType.DECIMAL;
    }

    @Override
    public boolean isShowRemark() {
        return false;
    }
}
